var candy = function (ratings) {
  let len = ratings.length;
  let left = (new Array(len)).fill(1);
  let right = (new Array(len)).fill(1);
  for(let i=1;i<len;i++){
    if(ratings[i]>ratings[i-1]){
      left[i]=left[i-1]+1;
    }
  }
  let count = Math.max(right[len-1],left[len-1]);
  for(let i=len-2;i>=0;i--){
    if(ratings[i]>ratings[i+1]){
      right[i]=right[i+1]+1;
    }
    count+=Math.max(right[i],left[i]);
  }

  return count;
};

let ratings1 = [1, 0, 2];
let ratings2 = [1, 2, 2];
let ratings3 = [1, 0, 2, 3, 2, 5, 1];
// 2,1,2,3,2,3,2
// 2,1,2
// 1,2,1
console.log(candy(ratings3));
